//
//  UserViewController.m
//  Doctor2.0
//
//  Created by mk on 15/6/9.
//  Copyright (c) 2015年 mk. All rights reserved.
//

#import "UserViewController.h"
#import "UIColor+AddColor.h"
#import "AboutUsViewController.h"
#import "AppointmentViewController.h"

@interface UserViewController ()
{
    NSMutableArray * arrayData;
}

@end

@implementation UserViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (NSVersionNumber_iOS_7_0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        UIView * viewNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, -64, appScreenWidth, 64)];
        viewNav.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:viewNav];
    }
    UILabel * titleNav = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 30)];
    titleNav.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18];
    titleNav.backgroundColor = [UIColor clearColor];
    titleNav.textAlignment = NSTextAlignmentCenter;
    titleNav.textColor = defaultNavBackColor;
    [self.navigationItem setTitleView:titleNav];
    titleNav.text = @"个人中心";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setViewData];
}

-(void)setViewData
{
    labTel.text = ([gProjConfig isLogin])?[NSString stringWithFormat:@"你好,%@",[gProjConfig.dicUserData objectForKey:USER_tel]]:@"你好,请先登录";
    
    btnAvatar.userInteractionEnabled = ([gProjConfig isLogin]?NO:YES);
    
    //列表
    for (NSLayoutConstraint *constraints in [viewAll constraints])
    {
        if (constraints.firstItem == viewAll && constraints.firstAttribute == NSLayoutAttributeHeight)
        {
            constraints.constant =  ([gProjConfig isLogin])?200:150;
            break;
        }
    }
}


-(IBAction)btnLogin:(id)sender
{
    if (![gProjConfig isLogin])
    {
        LoginViewController * log = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
        log.detegate = self;
        MyNavigationController *navi  = [[MyNavigationController alloc]initWithRootViewController:log];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
    }
}

//LoginViewControllerdelegate
-(void)loginSuccess
{
    [self setViewData];
}

-(IBAction)btnClick:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    if (btn.tag == 0)
    {
        if (![gProjConfig needLogin])
        {
            //预约单
            AppointmentViewController *view = [[AppointmentViewController alloc]initWithNibName:@"AppointmentViewController" bundle:nil];
            [self.navigationController pushViewController:view animated:YES];
        }
    }
    else if (btn.tag == 1)
    {
        AboutUsViewController * user = [[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:user animated:YES];
    }
    else if (btn.tag == 2)
    {
        UIAlertView * aler = [[UIAlertView alloc]initWithTitle:alerTitle message:@"拨打名医主刀热线电话:400-119-7900" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拨打", nil];
        aler.tag = 100;
        [aler show];
    
    }else if (btn.tag == 3)
    {
        AlertDelegate(alerTitle, @"确定要退出当前登录么?", @"确定", @"取消")
    }

    
}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100)
    {
        if (alertView.cancelButtonIndex != buttonIndex)
        {
            NSString * tel = [NSString stringWithFormat:@"tel://4001197900"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }else
    {
        if (alertView.cancelButtonIndex == buttonIndex)
        {
            [gProjConfig.dicUserData removeAllObjects];
            [gProjConfig.dicUserData setObject:@"" forKey:USER_tel];
            [gProjConfig.dicUserData setObject:@"" forKey:USER_token];
            [gProjConfig writeToFile];
            [self setViewData];
        }
    }

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
